var BaseTutorRegStep2 = app.controller('BaseTutorRegStep2', function($http, $scope, $rootScope, $location,$cordovaDevice,$cordovaChat,BaseTutorRegStep2Data) {
	try{
		$rootScope.showf=false;
		$scope.step=[];
		$scope.type=[];
		$scope.busy=false;

		//获取企业阶段（可选）
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=companystepcan";
		$http.jsonp(url).success(function(data) {
			$scope.step=data.retval;
			for ( var i = 0; i < $scope.step.length; i++) {
				if($scope.step[i].h>0){
					var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=fcategorystep&t="+$scope.step[i].d_value;
					$http.jsonp(url).success(function(data) {
						if(data.done){
							$scope.type.push(data.retval);
						}
					});
					$scope.step[i].h=1;
				}
			}
		});	

		//选择企业阶段
		$scope.loadtype=function(t){
			if($scope.busy){
				return;
			}
			$scope.busy=true;
			var n=t.s;
			if(n.h>0){
				if($scope.type.length>0){
					for(var i=0;i<$scope.type.length;i++){
						if($scope.type[i].t==n.d_value){
							$scope.type.splice(i,1);
							break;
						}
					}
				}
				$scope.step[t.$index].h=0;
				$scope.busy=false;
			}else{
				var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=fcategorystep&t="+n.d_value;
				$http.jsonp(url).success(function(data) {
					if(data.done){
						$scope.type.push(data.retval);
					}
					$scope.busy=false;
				});
				$scope.step[t.$index].h=1;
			}
		}

		//选择擅长领域
		$scope.choosecate=function(t){
			var n=t.d;
			if(n.h>0){
				$scope.type[t.$parent.$index].data[t.$index].h=0;
			}else{
				var h=0;
				for(var i=0;i<$scope.type.length;i++){
					if($scope.type[i].t==n.company_step){
						for(var j=0;j<$scope.type[i].data.length;j++){
							if($scope.type[i].data[j].h>0){
								h++;
							}
						}
					}
				}
				if(h>=4){
					showtoast('每个阶段最多选择4个领域');
					return false;
				}

				$scope.type[t.$parent.$index].data[t.$index].h=1;
			}
			if($scope.type.length>0){
				for(var i=0;i<$scope.type.length;i++){
					if($scope.type[i].t==t){
						$scope.type.splice(i,1);
						break;
					}
				}
			}
		}
		//下一步 跳转至第三步
		$scope.next=function(){
			var ids='';
			if($scope.type!=null&&$scope.type.length>0){
				for ( var i = 0; i < $scope.type.length; i++) {
					var isse=false;
					for ( var j = 0; j < $scope.type[i].data.length; j++) {
						if($scope.type[i].data[j].h>0){
							ids+=$scope.type[i].data[j].cate_id+",";
							isse=true;
						}
						if(j==$scope.type[i].data.length-1&&$scope.type[i].data[j].h<=0&&!isse){
							showtoast('每个阶段请至少选择一项领域');
							return;
						}
					}
				}
				var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=checkgoodat&ids="+ids;
				$http.jsonp(url).success(function(data) {
					if(data.done){
						$rootScope.go('tutorregstep3');
					}
				});
			}else{
				showtoast('请至少选择一个阶段');
			}
		}
	}
	catch (e) {
		showcatch(e);
	}
});
BaseTutorRegStep2.BaseTutorRegStep2Data=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}
		
		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}